// 牲畜溯源

var express = require('express');
var router = express.Router();
const db = require('../../db/db');

// 健康信息(历史记录)
router.get('/getHealthInfo', function(req, res, next) {
  const { liveid } = req.query;

  // 验证 liveid 是否存在
  if (!liveid) {
    return res.status(400).send({ code: 0, message: '缺少 liveid 参数' });
  }

  const sql = `SELECT * FROM nc_growinfo WHERE liveid = ?`;

  db.query(sql, [liveid], (err, result) => {
    console.log(sql);

    if (err) {
      console.error('Error querying data: ' + err.stack);
      return res.status(500).send({ code: 0, message: '获取失败', error: err });
    } else {
      res.send({ code: 200, message: '获取成功', data: result });
    }
  });
});

// 健康信息(最新记录)
router.get('/getHealthInfoNew', function(req, res, next) { 
    const { liveid } = req.query;
    // 验证 liveid 是否存在
    if (!liveid) {
      return res.status(400).send({ code: 0, message: '缺少 liveid 参数' });
    }
    const sql = `SELECT * FROM nc_growinfo_real WHERE liveid = ? `;
    db.query(sql, [liveid], (err, result) => {
        console.log(sql);
        if (err) {
            console.error('Error querying data: ' + err.stack);
            return res.status(500).send({ code: 0, message: '获取失败', error: err });
        } else {
            res.send({ code: 200, message: '获取成功', data: result });
        }
    });
});

//疫苗查询，单个
router.get('/getVaccine', function(req, res, next) {
  const { F_liveid } = req.query;

  // 验证 liveid 是否存在
  if (!F_liveid) {
    return res.status(400).send({ code: 0, message: '缺少 liveid 参数' });
  }

  const sql = `SELECT * FROM nc_vaccine_log WHERE F_liveid = ?`;

  db.query(sql, [F_liveid], (err, result) => {
    console.log(sql);

    if (err) {
      console.error('Error querying data: ' + err.stack);
      return res.status(500).send({ code: 0, message: '获取失败', error: err });
    } else {
      res.send({ code: 200, message: '获取成功', data: result });
    }
  });
});








module.exports = router;